#############################################################################
class InstStat:
	def __init__(self):
		self.stat={}
	def GetInstanceStatistics(self):
		from ET import GetInstanceStatistics
		return GetInstanceStatistics()

	def StartStat(self):
		list=self.GetInstanceStatistics()
		for i in list:
			cln,sz,cnt=i
			self.stat[cln]=cnt
		print "Start Statistics"

	def PrintInstStat(self):
		list=self.GetInstanceStatistics()
		nl=[]
		for i in list:
			nl.append((i[2],i))
		nl.sort()
		list=[]
		for i in nl:
			if i[1][2]:
				list.append(i[1])
		self.PrintList(list)

	def PrintList(self,list):
		print "\n", 23* "=", "Total Instances", 24* "="
		byteSum=0
		instSum=0
		classSum=0
		for i in list:
			cln,sz,cnt=i
			instSum=instSum+cnt
			classSum=classSum+sz
			byteSum=byteSum+sz*cnt
		print "%30s %6s %6s %10s %5s" %("Class","clSize","clCnt","Bytes","% relative to total Bytes")
		for i in list:
			cln,sz,cnt=i
			print "%30s %6d %6d %10d (%5.2f%%)" %(cln,sz,cnt,sz*cnt,100.0*sz*cnt/byteSum)
		print 77* "-"
		print "%30s %6d %6d %10d" %("TOTAL",classSum,instSum,byteSum)

	def PrintDiffList(self,list):
		print "\n", 30* "=", "Differences", 30* "="
		byteSum=0
		instSum=0
		classSum=0
		diffSum=0
		for i in list:
			cln,diff,cnt,sz=i
			instSum=instSum+cnt
			classSum=classSum+sz
			byteSum=byteSum+sz*diff
			diffSum=diffSum+diff
		print "%30s %6s %6s %7s %10s %5s" %("Class","clSize","diffCnt","newCnt","diffBytes","% relative to total diffBytes")
		for i in list:
			cln,diff,cnt,sz=i
			print "%30s %6d %6d (%6d) %10d (%5.2f%%)" %(cln,sz,diff,cnt,sz*diff,100.0*sz*diff/byteSum
)
		print 77* "-"
		print "%30s %6d %6d (%6d) %10d" %("TOTAL DIFFERENCES",classSum,diffSum,instSum,byteSum)

	def DiffStat(self):
		if len(self.stat)==0:
			self.StartStat()
			print "Start Statistics"
			return
		list=[]
		stat=self.stat
		for i in self.GetInstanceStatistics():
			cln,sz,cnt=i
			if cnt-stat[cln] !=0:
				list.append(cln,cnt-stat[cln],cnt,sz)
		list.sort(lambda x,y:cmp(x[1],y[1]))
		self.PrintDiffList(list)

instStat=InstStat()
